package cn.pxs.service;

import cn.pxs.entity.LargeFile;
import cn.pxs.entity.NewBook;
import cn.pxs.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
//书籍查询
public class BookServiceImpl  implements BookService {
    @Autowired
     BookMapper bookMapper;
    @Autowired
    LargeFileService largeFileService;

    @Override
    //全查询
    public List<NewBook> findAll() {
        return bookMapper.findAll();
    }

    @Override
    //根据id查询
    public NewBook findById(Integer id) {
        return bookMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //分页查询
    public List<NewBook> findByPage(Integer start,Integer num) {
        return bookMapper.findByPage(start, num);
    }

    @Override
    //分页的行数
    public int findByLine() {
        return bookMapper.findByLine();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加书本
    public void add(LargeFile largeFile, NewBook newBook){
        bookMapper.add(newBook);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加书本 或者  修改 书本
    public NewBook updateOrAdd(MultipartFile file,LargeFile largeFile, NewBook newBook) {
        try {
            if (file != null ) {
                //创建图片
                String uuid = UUID.randomUUID().toString();
                largeFile.setId(uuid);
                largeFile.setFilename(file.getOriginalFilename());
                byte[] bytes = file.getBytes();
                largeFile.setContent(bytes);//存图片数据
                System.out.println(largeFile);
            }
        }catch(Exception e){
            System.out.println(e);
        }
        if(newBook.getId()==null) {
            //如果没 id 就 添加
            newBook.setPicture(largeFile.getId());//写入图片id
            bookMapper.add(newBook); //进行添加
            largeFileService.add(largeFile);
            return bookMapper.findById(newBook.getId());
        }
        if(newBook.getId()!=null){
            //如果有id 就 修改
            if (file !=null){
                //如果修改图片
                NewBook b1 = bookMapper.findById(newBook.getId());//找到这个书
                //如果添加的时候bookType.id 为空，导致数据库的typeid为空，会查询不到该书本
                largeFileService.delById(b1.getPicture());//删掉他原来的图片
                largeFileService.add(largeFile);//添加新的图片
                newBook.setPicture(largeFile.getId());//书本的picture换新的
            }
            bookMapper.update(newBook);
            return bookMapper.findById(newBook.getId());
        }
        return null;
    }


    @Override
    //typeid 的行数
    public int findByTypeIDLine(Integer typeid) {
        return bookMapper.findByTypeIDLine(typeid);
    }

    @Override
    //修改点赞
    public void updateRate(Integer id) {
        NewBook book = bookMapper.findById(id);
        if(null == book.getRate()) book.setRate(0);
        //每进行一次就+1
        int rate = book.getRate()+1;
        bookMapper.updateRate(rate,id);
    }

    @Override
    //书籍分页查找(typeid)
    public List<NewBook> findBookByPage(Integer start, Integer num,Integer typeid) {
        return bookMapper.findBookByPage(start, num, typeid);
    }

    @Override
    //查salenum前五
    public List<NewBook> findSale5() {
        return bookMapper.findSale5();
    }

    @Override
    //点赞Rate前五
    public List<NewBook> findRate5() {
        return bookMapper.findRate5();
    }

    @Override
    //购物车前五
    public List<NewBook> findFav5() {
        return bookMapper.findFav5();
    }

    @Override
    //查找书本信息 Rate 高到低
    public List<NewBook> findBookByPageRate(Integer start, Integer num, Integer typeid) {
        return bookMapper.findBookByPageRate(start, num, typeid);
    }

    @Override
    //查找书本信息 Sale 高到低
    public List<NewBook> findBookByPageSale(Integer start, Integer num, Integer typeid) {
        return bookMapper.findBookByPageSale(start, num, typeid);
    }

    @Override
    //查找书本信息 Fav 高到低
    public List<NewBook> findBookByPageFav(Integer start, Integer num, Integer typeid) {
        return bookMapper.findBookByPageFav(start, num, typeid);
    }

    @Override
    //模糊查询 书名 出版社 作者
    public List<NewBook> findIf(Integer start, Integer num, String key) {
        List<NewBook> books = bookMapper.findIf(start, num, key);
        if(books != null ){
            //查询到的书本不为空，才进行标红
            String str ="<span style='color:red;'>"+key+"</span>";
            for (NewBook b:books){
                //替换 数据 转为 红色标签
                if(b.getPublisher()!=null && b.getPublisher().matches(".*"+key+".*")){
                    b.setPublisher(b.getPublisher().replace(key,str));
                }
                if(b.getName()!=null&&b.getName().matches(".*"+key+".*")){
                    b.setName(b.getName().replace(key,str));
                }
                if(b.getAuthor()!=null&&b.getAuthor().matches(".*"+key+".*")){
                    b.setAuthor(b.getAuthor().replace(key,str));
                }
            }
        }
        return books;
    }

    @Override
    //模糊查询的行数
    public int findByIfIDLine(String key) {
        Map map = new HashMap();
        map.put("author",key);
        int line = bookMapper.findByIfIDLine(key);
        return line;
    }
}
